﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;
using System.Reflection;
using System.Threading;
using Telerik.WinControls;
using Telerik.WinControls.UI;

namespace Bitis_KeToanThue_Pro_DungChung.Common
{
    public static class ClassXuLyChung
    {
        public static void showformbymenuloaddulieudatabase(string formname, string Fullname)
        {  /* Get Full Name of Form with assambly name */

            // string AssamblyName = Assembly.GetExecutingAssembly().GetName().Name;
         //   string Fullname = String.Format("{0}.{1}", AssamblyName, formname);

            /* Get its type */
            Type t = Type.GetType(Fullname, false, true);

            /* Get Object of Form By its Fullname */
            object Obj_Form = Activator.CreateInstance(t);
            foreach (Form f in Application.OpenForms)
            {
                if (f.Name == formname)
                {
                    f.Focus();
                    return;
                }
            }
            /* Cast the Object to Desire Type and Show it  */
            (Obj_Form as Form).Show();


        }
        public static Image GetIamgeToDataBase(object photoValue)
        {
            if (!Convert.IsDBNull(photoValue) && photoValue != null)
            {
                return  ConvertByeToImage((byte[])photoValue);
          
            }
            else
            {
                return null;
            }
        }
        public static byte[] ConvertImageToByte(string strPath)
        {
            if (string.IsNullOrEmpty(strPath))
            {
                return null;
            }
            else
            {
                FileStream FS = new FileStream(strPath, FileMode.Open, FileAccess.Read); //create a file stream object associate to user selected file 
                byte[] img = new byte[FS.Length]; //create a byte array with size of user select file stream length
                FS.Read(img, 0, Convert.ToInt32(FS.Length));//read user selected file stream in to byte array
                return img;
            }
        }
        public static Image ConvertByeToImage(byte[] byteimage)
        {
            MemoryStream ms = new MemoryStream(byteimage);//create memory stream by passing byte array of the image

            return Image.FromStream(ms);//set image property of the picture box by creating a image from stream 
        }
        public static void tuychontimkiemtrenluoi(RadTextBox textboxtimkiem, RadGridView gridviewcantim, int cotbatdau, int socotkhongtimkiem)
        {


            bool danhdau = false;
            if (string.IsNullOrEmpty(textboxtimkiem.Text))
            {
                for (int k = cotbatdau; k < gridviewcantim.ColumnCount - socotkhongtimkiem; k++)
                {
                    for (int j = 0; j < gridviewcantim.RowCount; j++)
                    {
                        gridviewcantim.Rows[j].IsVisible = true;

                        gridviewcantim.Rows[j].Cells[k].Style.Reset();
                        // radGridView1.Rows[j].InvalidateRow();


                    }
                }
            }
            else
            {
                // loadgridbbtc(GetSafeString(multicolumnBienBanXuLy.SelectedValue), GetSafeString(multicolumBienBanTaiChe.SelectedValue));
                for (int i = 0; i < gridviewcantim.RowCount; i++)
                {
                    for (int h = cotbatdau; h < gridviewcantim.ColumnCount - socotkhongtimkiem; h++)
                    {


                        string text = gridviewcantim.Rows[i].Cells[h].Value.ToString();
                        if (text.IndexOf(textboxtimkiem.Text, 0, StringComparison.InvariantCultureIgnoreCase) >= 0)
                        {
                            gridviewcantim.Rows[i].IsVisible = true;
                            gridviewcantim.Rows[i].Cells[h].Style.CustomizeFill = true;
                            gridviewcantim.Rows[i].Cells[h].Style.DrawFill = true;
                            gridviewcantim.Rows[i].Cells[h].Style.BackColor = Color.FromArgb(201, 252, 254);
                            //  UpdatePanelInfoF();
                            danhdau = true;
                        }
                        else
                        {


                            gridviewcantim.Rows[i].Cells[h].Style.Reset();
                            gridviewcantim.Rows[i].InvalidateRow();
                        }

                    }
                    if (danhdau == false)
                    {
                        gridviewcantim.Rows[i].IsVisible = false;
                    }
                    danhdau = false;
                }


            }
        }
        public static bool sosanhngaythang(DateTime TuNgay, DateTime DenNgay)
        {

            TimeSpan ts = DenNgay.Date - TuNgay.Date;
            if (ts.Days < 0)
            {
                return false;

            }
            else
            {
                return true;
            }
        }
        public static int GetSafeBoolInt(object value)
        {
            if (value == null)
            {
                return 0;
            }
            else
            {
                try
                {
                    if ((bool)value == true)
                    {
                        return 1;
                    }
                    else
                    {
                        return 0;
                    }
                }
                catch
                {
                    if ((int)value == 1)
                    {
                        return 1;
                    }
                    else
                    {
                        return 0;
                    }
                }
            }

        }
        public static bool GetSafeBool(object value)
        {
            if (value == null)
            {
                return false;
            }
            else
            {
                try
                {
                    if ((bool)value == true)
                    {
                        return true;
                    }
                    else
                    {
                        return false;
                    }
                }
                catch
                {
                    if ((int)value == 1)
                    {
                        return true;
                    }
                    else
                    {
                        return false;
                    }
                }
            }

        }
        public static string GetSafeString(object value)
        {
            if (value == null)
            {
                return string.Empty;
            }
            return value.ToString();
        }
        public static string GetSafeFloatString(object value)
        {
            if (value == null)
            {
                return "0";
            }
            else
            {
                try
                {
                    return value.ToString();
                }
                catch
                {
                    return "0";
                }
            }
        }
        public static float GetSafeFloat(object value)
        {
            if (value == null)
            {
                return 0;
            }
            else
            {
                try
                {
                    return float.Parse(value.ToString());
                }
                catch
                {
                    return 0;
                }
            }
        }
        public static int GetSafeInt(object value)
        {
            if (value == null)
            {
                return 0;
            }
            else
            {
                try
                {
                    return int.Parse(value.ToString());
                }
                catch
                {
                    return 0;
                }
            }
        }
        public static string GetSafeIntString(object value)
        {
            if (value == null)
            {
                return "0";
            }
            else
            {
                try
                {
                    return value.ToString();
                }
                catch
                {
                    return "0";
                }
            }
        }
        public static void loadcombodungchung(RadDropDownList cbDungChung, DataTable dtb, string diplaymem, string valuemem)
        {

            if (dtb.Rows.Count > 0)
            {
                cbDungChung.DisplayMember = diplaymem;
                cbDungChung.ValueMember = valuemem;
                cbDungChung.DataSource = dtb;
            }

        }
        public static void setgiaodien(int theme, Form f)
        {

            //ThemeResolutionService.ApplyThemeToControlTree(this, fr.Name);
            // RadMessageBox.SetThemeName(fr.Name);



            if (theme == 0)
            {
                Theme Desert = new Theme("Desert");
                ThemeResolutionService.ApplyThemeToControlTree(f, Desert.Name);
                RadMessageBox.SetThemeName(Desert.Name);
            }
            else
            {
                if (theme == 1)
                {

                    Theme Breezet = new Theme("Breezet");
                    ThemeResolutionService.ApplyThemeToControlTree(f, Breezet.Name);
                    RadMessageBox.SetThemeName(Breezet.Name);
                }
                else// (frmTienIch.doinen == 2)
                {
                    Theme TelerikMetroBlue = new Theme("TelerikMetroBlue");
                    ThemeResolutionService.ApplyThemeToControlTree(f, TelerikMetroBlue.Name);
                    RadMessageBox.SetThemeName(TelerikMetroBlue.Name);

                }
            }




        }
        public static void openformcloseformfinal(string formname, string projectname, Form f)
        {
            Thread t = new Thread(new ThreadStart(
              delegate()
              {
                  ThreadProc(formname, projectname);
              }


              ));
            t.SetApartmentState(ApartmentState.STA);
            t.Start();

            f.Close();
        }
        public static void ThreadProc(string formname, string projectname)
        {
            Form t = layformbyproject(formname, projectname);

            bool check = false;
            foreach (Form f in Application.OpenForms)
            {
                if (f.Name == formname)
                {
                    check = true;
                }
            }
            if (check == true)
            {
                t.Focus();
                check = false;
            }
            else
            {
                Application.Run(t);
            }

        }

        public static Form layformbyproject(string formname, string AssamblyName)
        {  /* Get Full Name of Form with assambly name */

            // string AssamblyName = Assembly.GetExecutingAssembly().GetName().Name;
            string Fullname = String.Format("{0}.{1}", AssamblyName, formname);

            /* Get its type */
            Type t = Type.GetType(Fullname, false, true);

            /* Get Object of Form By its Fullname */
            object Obj_Form = Activator.CreateInstance(t);

            /* Cast the Object to Desire Type and Show it  */
            return Obj_Form as Form;


        }
        public static Form layform(string formname)
        {  /* Get Full Name of Form with assambly name */

            string AssamblyName = Assembly.GetExecutingAssembly().GetName().Name;
            string Fullname = String.Format("{0}.{1}", AssamblyName, formname);

            /* Get its type */
            Type t = Type.GetType(Fullname, false, true);

            /* Get Object of Form By its Fullname */
            object Obj_Form = Activator.CreateInstance(t);
            return Obj_Form as Form;
            /* Cast the Object to Desire Type and Show it  */
            //(Obj_Form as Form).Show();


        }
        public static void showformbyformnamebyproject(string formname, string AssamblyName)
        {  /* Get Full Name of Form with assambly name */

            // string AssamblyName = Assembly.GetExecutingAssembly().GetName().Name;
            string Fullname = String.Format("{0}.{1}", AssamblyName, formname);

            /* Get its type */
            Type t = Type.GetType(Fullname, false, true);

            /* Get Object of Form By its Fullname */
            object Obj_Form = Activator.CreateInstance(t);
            foreach (Form f in Application.OpenForms)
            {
                if (f.Name == formname)
                {
                    f.Focus();
                    return;
                }
            }
            /* Cast the Object to Desire Type and Show it  */
            (Obj_Form as Form).Show();


        }
        public static void closeformbyformnamebyproject(string formname, string AssamblyName)
        {  /* Get Full Name of Form with assambly name */

            // string AssamblyName = Assembly.GetExecutingAssembly().GetName().Name;
            string Fullname = String.Format("{0}.{1}", AssamblyName, formname);

            /* Get its type */
            Type t = Type.GetType(Fullname, false, true);

            /* Get Object of Form By its Fullname */
            object Obj_Form = Activator.CreateInstance(t);

            /* Cast the Object to Desire Type and Show it  */
            (Obj_Form as Form).Close();


        }
     

        public static void showformbyformname(string formname)
        {  /* Get Full Name of Form with assambly name */

            string AssamblyName = Assembly.GetExecutingAssembly().GetName().Name;
            string Fullname = String.Format("{0}.Form.{1}", AssamblyName, formname);

            /* Get its type */
            Type t = Type.GetType(Fullname, false, true);

            /* Get Object of Form By its Fullname */
            object Obj_Form = Activator.CreateInstance(t);
            foreach (Form f in Application.OpenForms)
            {
                if (f.Name == formname)
                {
                    f.Focus();
                    return;
                }
            }
            /* Cast the Object to Desire Type and Show it  */
            (Obj_Form as Form).Show();


        }

       /* private void CloseAllToolStripMenuItem_Click(object sender, EventArgs e)
        {
            foreach (Form childForm in MdiChildren)
            {
                childForm.Close();
            }
        }
        public bool checkMdiChildren(string frmFormName) // Kieu tra ve la bool
        {
            if (this.MdiChildren.Length > 0)  //this.MdiChildren.Length so cua so con
            {
                Form[] frm = this.MdiChildren;//Khai bao 1 mang form
                for (int i = 0; i < this.MdiChildren.Length; i++) // Chay tu cua so 0 den this.MdiChildren.Length
                    if (frm[i].Name == frmFormName) //Kiem tra co ten form i chua
                        return true; //tra ve ngay
            }
            return false;
        }*/
    }
}
